package col

import (
	"strings"
	"yunj/pkg/yunj/core/builder/builderconsts"
	tableConsts "yunj/pkg/yunj/core/builder/builderconsts"
	"yunj/pkg/yunj/core/builder/table"
	"yunj/pkg/yunj/util"
)

// 地区
type Area struct {
	table.Col
	Acc string `json:"acc"` // 地区精度
}

func NewArea(field string, title ...string) *Area {
	c := &Area{}
	// 设置默认值
	c.Type = tableConsts.TABLE_COL_TYPE_NORMAL

	table.SetColInstance(&c.Col, c)
	c.SetColField(field)
	if len(title) > 0 {
		c.SetTitle(title[0])
	}
	return c
}

// 设置标题
func (c *Area) SetTitle(title string) *Area {
	c.Title = title
	return c
}

// 设置排列方式
func (c *Area) SetAlign(align string) *Area {
	c.Align = align
	return c
}

// 设置固定列
func (c *Area) SetFixed(fixed string) *Area {
	c.Fixed = fixed
	return c
}

// 设置隐藏列
func (c *Area) SetHide(hide string) *Area {
	c.Hide = hide
	return c
}

// 设置宽度
func (c *Area) SetWidth(width any) *Area {
	c.Width = width
	return c
}

// 设置最小宽度
func (c *Area) SetMinWidth(minWidth uint64) *Area {
	c.MinWidth = minWidth
	return c
}

// 设置最大宽度
func (c *Area) SetMaxWidth(maxWidth uint64) *Area {
	c.MaxWidth = maxWidth
	return c
}

// 设置权限
func (c *Area) SetAuth(auth string) *Area {
	c.Auth = auth
	return c
}

// 处理属性
func (c *Area) HandleAttr() error {
	if c.Acc == "" {
		c.Acc = builderconsts.AREA_ACC_DISTRICT
	}
	return nil
}

// 获取导出值
func (c *Area) GetExportValue(value any) any {
	var valueMap map[string]string
	var ok bool
	if valueMap, ok = value.(map[string]string); !ok {
		if valueStr, ok := value.(string); ok {
			if _valueMap, err := util.JsonTo[map[string]string](valueStr); err == nil {
				valueMap = _valueMap
			}
		}
	}
	if valueMap == nil {
		return value
	}
	// 获取area可选地区数据内容
	areaOptions := util.GetAreaOptions()
	newValueSlice := []string{}

	//  获取省
	provinceValue, exists := valueMap["province"]
	if !exists || provinceValue == "" {
		return value
	}
	provinceOptions, exists := (*areaOptions)["0"]
	if !exists || provinceOptions == nil {
		return value
	}
	province, exists := provinceOptions[provinceValue]
	if !exists {
		return value
	}
	newValueSlice = append(newValueSlice, province)
	if c.Acc == builderconsts.AREA_ACC_PROVINCE {
		return strings.Join(newValueSlice, "/")
	}
	// 获取市
	cityValue, exists := valueMap["city"]
	if !exists || cityValue == "" {
		return value
	}
	cityOptions, exists := (*areaOptions)["0,"+provinceValue]
	if !exists || cityOptions == nil {
		return value
	}
	city, exists := cityOptions[cityValue]
	if !exists {
		return value
	}
	newValueSlice = append(newValueSlice, city)
	if c.Acc == builderconsts.AREA_ACC_CITY {
		return strings.Join(newValueSlice, "/")
	}
	// 获取区/县
	districtValue, exists := valueMap["district"]
	if !exists || districtValue == "" {
		return value
	}
	districtOptions, exists := (*areaOptions)["0,"+provinceValue+","+cityValue]
	if !exists || districtOptions == nil {
		return value
	}
	district, exists := districtOptions[districtValue]
	if !exists {
		return value
	}
	newValueSlice = append(newValueSlice, district)

	return strings.Join(newValueSlice, "/")
}
